module.exports = (app) => {
    const mongoose = app.mongoose;
    mongoose.pluralize(null)
    const Schema = mongoose.Schema;
    const AccountSchema = new Schema({
        // 商品id
        goodsId: {
            type: mongoose.Types.ObjectId,
            ref: "Goods",
            required: true
        },
        // 折扣度
        account: {
            type: Number
        },
        // 是否第二杯半价
        isHalfPrice: {
            type: Boolean,
            default: false
        },
        // 优惠价
        privilegePrice: {
            type: Number
        },
        // 支付价
        payPrice: {
            type: Number,
        },
        // // 减价
        // minusPrice: {
        //     type: Number,
        // },
        // 优惠方式（1：折扣，2：特价优惠，3：第二杯半价
        method: {
            type: Number,
            required: true
        },
        // 活动持续时间
        activityDate: {
            type: [String],
            required: true
        },
        // 状态，1：未开始，2：生效中，3：已结束
        status: {
            type: Number,
            default: 1
        }
    }, { versionKey: false })

    AccountSchema.pre("save", function (next) {
        const date = new Date();
        if (this.activityDate[0] < date) {
            this.status = 1;
        } else if (this.activityDate[1] < date) {
            this.status = 3;
        } else {
            this.status = 2;
        }
        next();
    })
    return mongoose.model("Account", AccountSchema)
}